{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "c5oL4Cc9lwHR"
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib as mpl\n",
    "from matplotlib import pylab as plt\n",
    "import matplotlib.dates as mdates\n",
    "import seaborn as sns\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"..\") # Adds higher directory to python modules path.\n",
    "import ta as ta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def addTechnicals(data):\n",
    "    close = np.array(data['Close'].values)\n",
    "    dates = np.array(data['Date'], dtype='datetime64[D]')\n",
    "    kama = ta.kama(data['Close'].values)\n",
    "    return close, dates, kama"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>2015-07-30</td>\n",
       "      <td>112.080002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>2015-07-31</td>\n",
       "      <td>111.949997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>2015-08-03</td>\n",
       "      <td>111.599998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>2015-08-04</td>\n",
       "      <td>111.389999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>2015-08-05</td>\n",
       "      <td>112.250000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Date       Close\n",
       "144  2015-07-30  112.080002\n",
       "145  2015-07-31  111.949997\n",
       "146  2015-08-03  111.599998\n",
       "147  2015-08-04  111.389999\n",
       "148  2015-08-05  112.250000"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series = pd.read_csv('qqq_kama.csv')\n",
    "\n",
    "\n",
    "series.tail()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "close, dates, kama = addTechnicals(series)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "height": 418
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 915,
     "status": "ok",
     "timestamp": 1552960592858,
     "user": {
      "displayName": "",
      "photoUrl": "",
      "userId": ""
     },
     "user_tz": 420
    },
    "id": "--MhD5M4WgMy",
    "outputId": "3f9a839a-6a67-4650-aeb3-7dda1f2db7aa",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>close</th>\n",
       "      <th>dates</th>\n",
       "      <th>kama</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>102.940002</td>\n",
       "      <td>2015-01-02</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>101.430000</td>\n",
       "      <td>2015-01-05</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>100.070000</td>\n",
       "      <td>2015-01-06</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>101.360001</td>\n",
       "      <td>2015-01-07</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>103.300003</td>\n",
       "      <td>2015-01-08</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>102.620003</td>\n",
       "      <td>2015-01-09</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>101.550003</td>\n",
       "      <td>2015-01-12</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>101.519997</td>\n",
       "      <td>2015-01-13</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>100.959999</td>\n",
       "      <td>2015-01-14</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>99.650002</td>\n",
       "      <td>2015-01-15</td>\n",
       "      <td>99.650002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>100.820000</td>\n",
       "      <td>2015-01-16</td>\n",
       "      <td>99.688509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>101.620003</td>\n",
       "      <td>2015-01-20</td>\n",
       "      <td>99.699584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>102.139999</td>\n",
       "      <td>2015-01-21</td>\n",
       "      <td>99.794816</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>104.029999</td>\n",
       "      <td>2015-01-22</td>\n",
       "      <td>100.010700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>104.260002</td>\n",
       "      <td>2015-01-23</td>\n",
       "      <td>100.087570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>104.139999</td>\n",
       "      <td>2015-01-26</td>\n",
       "      <td>100.223849</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>101.440002</td>\n",
       "      <td>2015-01-27</td>\n",
       "      <td>100.230086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>100.919998</td>\n",
       "      <td>2015-01-28</td>\n",
       "      <td>100.237167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>101.889999</td>\n",
       "      <td>2015-01-29</td>\n",
       "      <td>100.260674</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>101.099998</td>\n",
       "      <td>2015-01-30</td>\n",
       "      <td>100.280692</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>101.980003</td>\n",
       "      <td>2015-02-02</td>\n",
       "      <td>100.313367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>102.959999</td>\n",
       "      <td>2015-02-03</td>\n",
       "      <td>100.371783</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>102.870003</td>\n",
       "      <td>2015-02-04</td>\n",
       "      <td>100.403374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>103.760002</td>\n",
       "      <td>2015-02-05</td>\n",
       "      <td>100.427293</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>103.129997</td>\n",
       "      <td>2015-02-06</td>\n",
       "      <td>100.483269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>102.800003</td>\n",
       "      <td>2015-02-09</td>\n",
       "      <td>100.539950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>104.400002</td>\n",
       "      <td>2015-02-10</td>\n",
       "      <td>100.879513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>104.779999</td>\n",
       "      <td>2015-02-11</td>\n",
       "      <td>101.421542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>106.000000</td>\n",
       "      <td>2015-02-12</td>\n",
       "      <td>102.090386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>106.910004</td>\n",
       "      <td>2015-02-13</td>\n",
       "      <td>103.328308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>119</th>\n",
       "      <td>110.220001</td>\n",
       "      <td>2015-06-24</td>\n",
       "      <td>109.589357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>120</th>\n",
       "      <td>110.000000</td>\n",
       "      <td>2015-06-25</td>\n",
       "      <td>109.593252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>121</th>\n",
       "      <td>109.269997</td>\n",
       "      <td>2015-06-26</td>\n",
       "      <td>109.589000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>122</th>\n",
       "      <td>106.690002</td>\n",
       "      <td>2015-06-29</td>\n",
       "      <td>109.500162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123</th>\n",
       "      <td>107.070000</td>\n",
       "      <td>2015-06-30</td>\n",
       "      <td>109.409075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>124</th>\n",
       "      <td>107.919998</td>\n",
       "      <td>2015-07-01</td>\n",
       "      <td>109.376922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>125</th>\n",
       "      <td>107.949997</td>\n",
       "      <td>2015-07-02</td>\n",
       "      <td>109.250024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>126</th>\n",
       "      <td>107.699997</td>\n",
       "      <td>2015-07-06</td>\n",
       "      <td>109.139456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>127</th>\n",
       "      <td>107.970001</td>\n",
       "      <td>2015-07-07</td>\n",
       "      <td>109.004369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>128</th>\n",
       "      <td>106.089996</td>\n",
       "      <td>2015-07-08</td>\n",
       "      <td>108.467452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>129</th>\n",
       "      <td>106.029999</td>\n",
       "      <td>2015-07-09</td>\n",
       "      <td>108.052669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>130</th>\n",
       "      <td>107.650002</td>\n",
       "      <td>2015-07-10</td>\n",
       "      <td>108.031717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>131</th>\n",
       "      <td>109.540001</td>\n",
       "      <td>2015-07-13</td>\n",
       "      <td>108.041635</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>132</th>\n",
       "      <td>110.260002</td>\n",
       "      <td>2015-07-14</td>\n",
       "      <td>108.290466</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>133</th>\n",
       "      <td>110.379997</td>\n",
       "      <td>2015-07-15</td>\n",
       "      <td>108.509409</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>134</th>\n",
       "      <td>111.940002</td>\n",
       "      <td>2015-07-16</td>\n",
       "      <td>108.936137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>135</th>\n",
       "      <td>113.589996</td>\n",
       "      <td>2015-07-17</td>\n",
       "      <td>109.693658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>113.980003</td>\n",
       "      <td>2015-07-20</td>\n",
       "      <td>110.511137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137</th>\n",
       "      <td>113.910004</td>\n",
       "      <td>2015-07-21</td>\n",
       "      <td>111.121106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>112.620003</td>\n",
       "      <td>2015-07-22</td>\n",
       "      <td>111.472461</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139</th>\n",
       "      <td>112.199997</td>\n",
       "      <td>2015-07-23</td>\n",
       "      <td>111.617408</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>111.099998</td>\n",
       "      <td>2015-07-24</td>\n",
       "      <td>111.573871</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>110.180000</td>\n",
       "      <td>2015-07-27</td>\n",
       "      <td>111.556609</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>111.129997</td>\n",
       "      <td>2015-07-28</td>\n",
       "      <td>111.549796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>111.550003</td>\n",
       "      <td>2015-07-29</td>\n",
       "      <td>111.549801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>112.080002</td>\n",
       "      <td>2015-07-30</td>\n",
       "      <td>111.552816</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>111.949997</td>\n",
       "      <td>2015-07-31</td>\n",
       "      <td>111.572617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>111.599998</td>\n",
       "      <td>2015-08-03</td>\n",
       "      <td>111.575023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>111.389999</td>\n",
       "      <td>2015-08-04</td>\n",
       "      <td>111.557854</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>112.250000</td>\n",
       "      <td>2015-08-05</td>\n",
       "      <td>111.565104</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>149 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          close      dates        kama\n",
       "0    102.940002 2015-01-02         NaN\n",
       "1    101.430000 2015-01-05         NaN\n",
       "2    100.070000 2015-01-06         NaN\n",
       "3    101.360001 2015-01-07         NaN\n",
       "4    103.300003 2015-01-08         NaN\n",
       "5    102.620003 2015-01-09         NaN\n",
       "6    101.550003 2015-01-12         NaN\n",
       "7    101.519997 2015-01-13         NaN\n",
       "8    100.959999 2015-01-14         NaN\n",
       "9     99.650002 2015-01-15   99.650002\n",
       "10   100.820000 2015-01-16   99.688509\n",
       "11   101.620003 2015-01-20   99.699584\n",
       "12   102.139999 2015-01-21   99.794816\n",
       "13   104.029999 2015-01-22  100.010700\n",
       "14   104.260002 2015-01-23  100.087570\n",
       "15   104.139999 2015-01-26  100.223849\n",
       "16   101.440002 2015-01-27  100.230086\n",
       "17   100.919998 2015-01-28  100.237167\n",
       "18   101.889999 2015-01-29  100.260674\n",
       "19   101.099998 2015-01-30  100.280692\n",
       "20   101.980003 2015-02-02  100.313367\n",
       "21   102.959999 2015-02-03  100.371783\n",
       "22   102.870003 2015-02-04  100.403374\n",
       "23   103.760002 2015-02-05  100.427293\n",
       "24   103.129997 2015-02-06  100.483269\n",
       "25   102.800003 2015-02-09  100.539950\n",
       "26   104.400002 2015-02-10  100.879513\n",
       "27   104.779999 2015-02-11  101.421542\n",
       "28   106.000000 2015-02-12  102.090386\n",
       "29   106.910004 2015-02-13  103.328308\n",
       "..          ...        ...         ...\n",
       "119  110.220001 2015-06-24  109.589357\n",
       "120  110.000000 2015-06-25  109.593252\n",
       "121  109.269997 2015-06-26  109.589000\n",
       "122  106.690002 2015-06-29  109.500162\n",
       "123  107.070000 2015-06-30  109.409075\n",
       "124  107.919998 2015-07-01  109.376922\n",
       "125  107.949997 2015-07-02  109.250024\n",
       "126  107.699997 2015-07-06  109.139456\n",
       "127  107.970001 2015-07-07  109.004369\n",
       "128  106.089996 2015-07-08  108.467452\n",
       "129  106.029999 2015-07-09  108.052669\n",
       "130  107.650002 2015-07-10  108.031717\n",
       "131  109.540001 2015-07-13  108.041635\n",
       "132  110.260002 2015-07-14  108.290466\n",
       "133  110.379997 2015-07-15  108.509409\n",
       "134  111.940002 2015-07-16  108.936137\n",
       "135  113.589996 2015-07-17  109.693658\n",
       "136  113.980003 2015-07-20  110.511137\n",
       "137  113.910004 2015-07-21  111.121106\n",
       "138  112.620003 2015-07-22  111.472461\n",
       "139  112.199997 2015-07-23  111.617408\n",
       "140  111.099998 2015-07-24  111.573871\n",
       "141  110.180000 2015-07-27  111.556609\n",
       "142  111.129997 2015-07-28  111.549796\n",
       "143  111.550003 2015-07-29  111.549801\n",
       "144  112.080002 2015-07-30  111.552816\n",
       "145  111.949997 2015-07-31  111.572617\n",
       "146  111.599998 2015-08-03  111.575023\n",
       "147  111.389999 2015-08-04  111.557854\n",
       "148  112.250000 2015-08-05  111.565104\n",
       "\n",
       "[149 rows x 3 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'close':close, 'dates':dates, 'kama':kama})\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [
    "5BVYddeJg-An"
   ],
   "last_runtime": {
    "build_target": "",
    "kind": "local"
   },
   "name": "Structural Time Series Modeling Case Studies: Atmospheric CO2 and Electricity Demand",
   "provenance": [
    {
     "file_id": "1ZrZXuGar-SJOCQZk64HxJ8pOSY2JLqzx",
     "timestamp": 1551736254082
    }
   ],
   "version": "0.3.2"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
